<?php


class lb_db extends lb_db_operation {
	
	protected $db;
	
	/**
	 * constructor of PDO class, this method will connect to DB
	 * 
	 * @access 	public
	 * @author	Omid Mottaghi
	 * @param	string	db host
	 * @param	string	db name
	 * @param	string	db username
	 * @param	string	db password
	 */
	function __construct ($host=false, $db=false, $user=false, $pass=false) {
		global $lb;
		
		if($host === false){
			$this->db = $this->db_connect();
		}else{
			$this->db = $this->db_connect($host=false, $db=false, $user=false, $pass=false);
		}
	}
	
	/**
	 * execute queries. this function should escape your variables before send them to DBMS
	 * 
	 * @access 	public
	 * @author	Omid Mottaghi
	 * @param	string	desired query, before send query, please replace ANY variable in the query, with # sign (don't add single quote around # sign)
	 * @param	array	a 2d array of variables and it's type that should checked, escaped and replaced with # in array order
	 * @return 	object	return PDOStatement of the query
	 */
	function query ($sql, $vars){
		global $lb;
		
		$sql = str_replace('#_', $lb['prefix'], $sql);
		
		$offset = 0;
		$num = substr_count($sql, '#');
		
		// if number of sql's #s are different from array cellule's number
		if (count($vars) != $num){
			//print('VARS NOT MATCHED...' . count($vars) . '!=' . $num .'<br /><br />');
			return false;
		}
		
		$sql_arr = explode('#', $sql);
		$sql = '';
		
		// check and escape variables
		for ($i=0; $i<$num; $i++){
			$quote = true;//means at default need to quote input vars
			$vars[$i][1] = strtolower ($vars[$i][1]);
			switch ($vars[$i][1]) {
				case 'boolean':
					if(!is_bool($vars[$i][0])){
						$lb['error'][] = sprintf(LANG_MYSQL_QUERY_ERROR, $sql);
						return false;
					}
					$quote = false;
					break;
					
				case 'int':
				case 'integer':
					$vars[$i][0] = intval($vars[$i][0]);
					$quote = false;
					break;
					
				case 'real':
				case 'float':
					$vars[$i][0] = floatval($vars[$i][0]);
					$quote = false;
					break;
					
				case 'text':
				case 'char':
				case 'varchar':
					$vars[$i][0] = $this->db_escape_string($vars[$i][0]);
					break;
					
				case 'blob':
					$vars[$i][0] = $this->db_escape_string($vars[$i][0], true);
					break;
					
				case 'password':
				case 'hash':
					$vars[$i][0] = lb_system::hash($vars[$i][0]);
					break;
					
				default:
					break;
			}
			
			// replace with first # sign
			if($quote){
				$sql .= $sql_arr[$i] . '"' . $vars[$i][0] . '"';
			}else{
				$sql .= $sql_arr[$i] . $vars[$i][0];
			}
		}
		$sql .= $sql_arr[$i]; //Add last part of splited query to the end of the query
		
		//echo $sql.'<br><br>';
		
		$result['result'] = $this->db_query ($sql);
		
		//determine sql query is a SELECT query or not
		$sql_query = strtolower (ltrim ($sql));
		if (stripos ($sql_query, 'select') === 0 || stripos ($sql_query, 'show') === 0){
			$in = false;
		}else{
			$in = true;
		}
		
		if ($in){
			//INSERT or UPDATE or DELETE query result operation
			$result['insert_id']	= $this->db_insert_id();
			$result['num_of_rows'] 	= $this->db_affected_rows();
		}else{
			//SELECT query result operation
			$result['num_of_rows'] 	= $this->db_num_rows($result['result']);
			while( $row = $this->db_fetch_assoc($result['result'])){
				$result['rows'][] = $row;
			}
		}
		
		if($lb['debug'] && $result['result'] === false){
			//$lb['error'][] = sprintf(LANG_MYSQL_QUERY_ERROR, $sql);
		}
		
		// execute query and return result
		return $result;
	}
	
	/**
	 * List tables from system database
	 *
	 * @author	Armin Borumand
	 * @return	mixed	query result
	 */
	function list_tables(){
		global $lb;
		
		$query  = 'SHOW TABLES FROM ' . $lb['db_name'];
		
		return $this->query($query, array());
	}
	
	/**
	 * Optimize tables
	 *
	 * @author	Armin Borumand
	 */
	function optimize(){
		
		$tables = '';		
		$result = $this->list_tables();
		
		foreach($result['rows'] as $table){
			$tables  .= $table['Tables_in_loubia'] . ', ';
		}
		
		$tables = substr($tables, 0, strlen($tables) - 2);
		
		$query  = 'OPTIMIZE TABLE ' . $tables;
		$this->query($query, array());
		
	}


}

?>
